class Solution2 {
    public int calculateMinimumHP(int[][] dungeon) {
        //1. 创建表
        int m = dungeon.length;
        int n = dungeon[0].length;
        int[][] dp = new int[m+1][n+1];

        //2.初始化
        for(int col = 0; col < n+1; col++) {
            dp[m][col] = Integer.MAX_VALUE;
        }
        for(int row = 0; row < m+1; row++) {
            dp[row][n] = Integer.MAX_VALUE;
        }
        dp[m][n-1] = 1;

        //3.填表
        for(int row = m-1; row >= 0; row--) {
            for(int col = n-1; col >= 0; col--) {
                dp[row][col] = Math.min(dp[row+1][col], dp[row][col+1]) - dungeon[row][col];
                dp[row][col] = Math.max(1, dp[row][col]);
            }
        }

        //4.返回值
        return dp[0][0];
    }
}